package com.plugins;

import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Component;


/**
 * Project: tobe-better-coder
 * Package: com.plugins
 *
 * @author : Mr.xu
 * @date : 2023/9/25 23:01
 */
@Intercepts(
        {@Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class, RowBounds.class,ResultHandler.class, CacheKey.class, BoundSql.class}),
        @Signature(type = Executor.class,method = "query",args = {MappedStatement.class,Object.class, RowBounds.class,ResultHandler.class}),
        @Signature(type = Executor.class,method = "update",args = {MappedStatement.class,Object.class})}
)
@Component
public class SqlTimePlugin implements Interceptor {
    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        Object result = null;
        Long start = System.currentTimeMillis();
        try{
            result = invocation.proceed();
        }finally {
            Long end = System.currentTimeMillis();
            System.out.println("sql执行耗时"+(end-start)+"毫秒");
        }

        return result;
    }
}